//
//  M1LikeListVC.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/20.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1LikeListVC.h"
#import "M1SegmengtView.h"
#import "M1LikeMeVC.h"
#import "M1MyLikeVC.h"

@interface M1LikeListVC ()

@property (nonatomic, strong) M1LikeMeVC *m1_likeMeVC;

@property (nonatomic, strong) M1MyLikeVC *m1_myLikeVC;

@property (nonatomic, strong) UIViewController *m1_currentVC;

@end

@implementation M1LikeListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"表达心意";
    
    [self m1_createUI];
}

- (void)m1_createUI{
    
    M1SegmengtView *m1_segment = [[M1SegmengtView alloc] initWithFrame:CGRectMake(0, STATUS_HEIGHT + 44, SCREENWIDTH, 65)];
    [m1_segment changeLeftTitle:@"对我表达心意的" right:@"我表达心意的"];
    [self.view addSubview:m1_segment];
    
    [self addChildViewController:self.m1_likeMeVC];
    [self.view addSubview:self.m1_likeMeVC.view];
    self.m1_currentVC = self.m1_likeMeVC;
    
    m1_segment.m1_switchScopeBlock = ^(NSInteger selectedSegmentIndex) {
        if ((self.m1_currentVC == self.m1_likeMeVC && selectedSegmentIndex == 1)||(self.m1_currentVC == self.m1_myLikeVC && selectedSegmentIndex == 2)) {
            return;
        }else{
            if (selectedSegmentIndex == 1) {
                [self replaceController:self.m1_currentVC newController:self.m1_likeMeVC];
            }else{
                [self replaceController:self.m1_currentVC newController:self.m1_myLikeVC];
            }
        }
    };
}

- (M1LikeMeVC *)m1_likeMeVC{
    if (!_m1_likeMeVC) {
        _m1_likeMeVC = [[M1LikeMeVC alloc] init];
        _m1_likeMeVC.view.frame = CGRectMake(0, STATUS_HEIGHT + 44 + 65, SCREENWIDTH, SCREENHEIGHT);
    }
    return _m1_likeMeVC;
}

- (M1MyLikeVC *)m1_myLikeVC{
    if (!_m1_myLikeVC) {
        _m1_myLikeVC = [[M1MyLikeVC alloc] init];
        _m1_myLikeVC.view.frame = CGRectMake(0, STATUS_HEIGHT + 44 + 65, SCREENWIDTH, SCREENHEIGHT);
    }
    return _m1_myLikeVC;
}

- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0.3 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.m1_currentVC = newController;
        }else{
            
            self.m1_currentVC = oldController;
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
